package com.gaofei.user.controller;

import com.macro.cloud.domain.AccessTokenDto;
import com.macro.cloud.domain.Oauth2ClientUserDto;
import com.macro.cloud.service.Oauth2ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @Title: UserController
 * @Author gaofee
 * @Package com.gaofei.user.controller
 * @Date 2024/03/12 14:36
 * @description: 认证服务的接口
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private Oauth2ClientService oauth2ClientService;

    @RequestMapping("getLoginUrl")
    public String getLoginUrl(){
        return oauth2ClientService.getOauth2CodeUrl();
    }

    @RequestMapping("callBack")
    public Object callBack(String code){
        AccessTokenDto accessToken = oauth2ClientService.getAccessToken(code);
        //根据token获取用户
        Oauth2ClientUserDto currentUser = oauth2ClientService.getCurrentUser(accessToken);
        //将用户信息和token封装到map中返回
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",accessToken.getAccess_token());
        map.put("username", currentUser.getUser_name());

        return map;
    }
}
